package com.ugood.utils;

import java.math.BigDecimal;
import java.text.DecimalFormat;

public class NumberFormatUtils {

	private final static String ZERO = "0";
	
	private final static String DOT = ".";
	
	private final static String REG1 = "0+?$";
	private final static String REG2 = "[.]$";
	
	private final static String BLANK = "";
	
	public static final DecimalFormat DECIMAL_FORMAT = new DecimalFormat("0.00000000");
	public static final DecimalFormat DECIMAL_FORMAT_02 = new DecimalFormat("0.00");
	public static final DecimalFormat DECIMAL_FORMAT_16 = new DecimalFormat("0.0000000000000000");       
	
	/**
	 * 去除数字后多余的0
	 * @param obj
	 * @return
	 */
	public static String subZeroAndDot(Object obj){
		if(obj == null) {
			return ZERO;
		}
		String s = String.valueOf(obj);
		if(obj instanceof Number) {
			s = bigDecimalFormat((Number) obj);
		}
        if(s.indexOf(DOT) > 0){  
            s = s.replaceAll(REG1, BLANK);
            s = s.replaceAll(REG2, BLANK);  
        }  
        return s;  
    }  
	
	
	public static String bigDecimalFormat02(BigDecimal bigDecimal) {
		return DECIMAL_FORMAT_02.format(bigDecimal);
	}
	
	public static String bigDecimalFormat(Number number) {//stripTrailingZeros
		return  DECIMAL_FORMAT.format(number);
	}
	public static String bigDecimalFormat(BigDecimal bigDecimal) {//stripTrailingZeros
		return bigDecimal.stripTrailingZeros().toPlainString();
	}
	public static String bigDecimalFormat16(BigDecimal bigDecimal) {
		return DECIMAL_FORMAT_16.format(bigDecimal);
	}
	
}
